package com.cndw.kungfu.dao;

import java.util.Map;
import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;
import com.cndw.kungfu.domain.DataInfoPlayer;
import com.cndw.kungfu.domain.Player;


@DAO
public interface PlayerDao {
	
	public static final String TABLE = " kf_player ";
	
	public static final String FIELDS = " playerId,playerName,accountName,level,exp,expPool,expJar,gold,goldBing,goldTicke,asset,points,sex,vocationId,hp,mp,status," +
			"power,physical,agile,wit,attack,defense,score,hit,dodge,crit,tough,attackSpeed,hurtAdd,hurtRemove,hurtReflex,hurtAbsorb,critAdd,maxHp,maxMp," +
			"regTime,lastLogin,offLineTime,lastIp,onLineTime,sceneId,x,y,dress,shortcut,boxsGirds,holdGrids,unionId," +
			"bloodBag,magicBag,vipLevel,vipScore,knightLevel,knightExpired,platform,titleId,agent";
	
	
	public static final String TABLE_TEMP = "kf_data_player";
	
	public static final String FIELDS_TEMP = "playerId,cacheData,syncDBTime";
	
	public static final String FIELDS_DRUGS = "playerId,takeHpDrug,hpDrugAdd,takeAtkDrug,atkDrugAdd,takeDefDrug,defDrugAdd,takeSpdDrug,spdDrugAdd,takeHitDrug,hitDrugAdd,takeDogDrug,dogDrugAdd,takeCritDrug,critDrugAdd,takeToughDrug,toughDrugAdd";
	
	public static final String PLAYER_IDCARD_TABLE = " kf_player_idcard ";
	public static final String PLAYER_IDCARD_FIELD = " playerId,realName,idcard,addTime ";

	/**
	 * 新建立一个角色
	 * @param player
	 * @return
	 */
	@SQL("INSERT INTO "+TABLE+" ("+FIELDS+") VALUES(:p.playerId,:p.playerName,:p.accountName,:p.level,:p.exp,:p.expPool,:p.expJar," +
			":p.gold,:p.goldBing,:p.goldTicke,:p.asset,:p.points,:p.sex,:p.vocationId,:p.hp,:p.mp,:p.status," +
			":p.power,:p.physical,:p.agile,:p.wit,:p.attack,:p.defense,:p.score," +
			":p.hit,:p.dodge,:p.crit,:p.tough,:p.attackSpeed,:p.hurtAdd,:p.hurtRemove,:p.hurtReflex,:p.hurtAbsorb,:p.critAdd,:p.maxHp,:p.maxMp," +
			":p.regTime,:p.lastLogin,:p.offLineTime,:p.lastIp,:p.onLineTime,:p.sceneId," +
			":p.x,:p.y,:p.dress,:p.shortcut,:p.boxsGirds,:p.holdGrids,:p.unionId,:p.bloodBag,:p.magicBag," +
			":p.vipLevel,:p.vipScore,:p.knightLevel,:p.knightExpired,:p.platform,:p.titleId,:p.agent)")
	public void insert(@SQLParam("p") Player player);
	
	
	/**
	 * 更新角色数据
	 * @param player
	 * @return
	 */
	@SQL("UPDATE "+TABLE+" SET playerName=:p.playerName, level=:p.level, exp=:p.exp,expPool=:p.expPool,expJar=:p.expJar,gold=:p.gold,goldBing=:p.goldBing,goldTicke=:p.goldTicke," +
			"asset=:p.asset,points=:p.points,sex=:p.sex, vocationId=:p.vocationId, hp=:p.hp, mp=:p.mp,status=:p.status," +
			"power=:p.power,physical=:p.physical,agile=:p.agile,wit=:p.wit,attack=:p.attack," +
			"defense=:p.defense,score=:p.score," +
			"hit=:p.hit,dodge=:p.dodge,crit=:p.crit,tough=:p.tough,attackSpeed=:p.attackSpeed,hurtAdd=:p.hurtAdd,hurtRemove=:p.hurtRemove,hurtReflex=:p.hurtReflex,hurtAbsorb=:p.hurtAbsorb,critAdd=:p.critAdd,maxHp=:p.maxHp,maxMp=:p.maxMp," +
			"lastLogin=:p.lastLogin,offLineTime=:p.offLineTime,lastIp=:p.lastIp,onLineTime=:p.onLineTime,sceneId=:p.sceneId," +
			"x=:p.x,y=:p.y,dress=:p.dress,shortcut=:p.shortcut,boxsGirds=:p.boxsGirds,holdGrids=:p.holdGrids," +
			"unionId=:p.unionId,boxsGirds=:p.boxsGirds,holdGrids=:p.holdGrids,bloodBag=:p.bloodBag,magicBag=:p.magicBag," +
			"vipLevel=:p.vipLevel,vipScore=:p.vipScore,knightLevel=:p.knightLevel,knightExpired=:p.knightExpired," +
			"titleId=:p.titleId,agent=:p.agent WHERE playerId = :p.playerId limit 1")
	public int update(@SQLParam("p") Player player);
	
	/**
	 * 根据用户id获取角色信息
	 * @param playerId
	 * @return
	 */
	@SQL("SELECT "+FIELDS+" FROM "+TABLE+" WHERE playerId = :playerId ")
	public Player getByPlayerId(@SQLParam("playerId") long playerId);
	
	/**
	 * 根据帐号获取信息
	 * @param accountName
	 * @return
	 */
	@SQL("SELECT playerid FROM "+TABLE+" WHERE accountName = :accountName")
	public long getByAccountName(@SQLParam("accountName") String accountName);
	
	
	/**
	 * 根据角色名获取信息
	 * @param accountName
	 * @return
	 */
	@SQL("SELECT playerid FROM "+TABLE+" WHERE playerName = :playerName")
	public long getByPlayerName(@SQLParam("playerName") String playerName);
	
	/**
	 * 获取各个职业的数量
	 * @return
	 */
	@SQL("select vocationId,count(playerId) from "+TABLE+" group by vocationId ")
	public Map<Integer, Integer> getCountsGroup();

	
	@SQL("select max(playerId) from "+TABLE+" ")
	public long getMaxPlayerId();


	@SQL("update "+TABLE_TEMP+" set cacheData=:2,syncDBTime=:3 where playerId=:1  ")
	public void syncTempData(long playerId, String cacheData,int syncDBTime);

	@SQL("INSERT INTO "+TABLE_TEMP+" ("+FIELDS_TEMP+") VALUES(:1,:2,:3)")
	public void insertTempData(long playerId, String cacheData, int syncDBTime);
	
	@SQL("select "+FIELDS_TEMP+" from "+TABLE_TEMP+" where playerId =:1")
	public DataInfoPlayer getDataInfo(long playerId);
	
	@SQL("INSERT INTO "+PLAYER_IDCARD_TABLE+" ("+PLAYER_IDCARD_FIELD+") VALUES(:1,:2,:3,UNIX_TIMESTAMP())")
	public void insertPlayerIdCard(long playerId, String name,String idcard);

	@SQL("select playerId from "+TABLE+" order by RAND() limit 1 ")
	public long getRatePlayerId();
}
